package com.tuanbiao.ui.plan.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hzjq.data.entity.PlanEntity;
import com.tuanbiao.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author abiao
 * @since 2022/4/11 10:24
 */
public class PlanAdapter extends BaseQuickAdapter<PlanEntity, BaseViewHolder> {

    public PlanAdapter() {
        super(R.layout.item_detonator_plan);
    }

    private OnSelectChangeListener listener;

    public void setListener(OnSelectChangeListener listener) {
        this.listener = listener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, PlanEntity plan) {
        holder.setImageResource(R.id.iv_select
                , plan.isSelect() ? R.drawable.checkbox_checked_ic : R.drawable.compound_normal_ic);
        holder.setText(R.id.tv_plan_name, plan.getName())
                .setText(R.id.tv_auth_count, getContext().getString(R.string.detonator_plan_manage_adapter_auth_count, plan.getCapCount()))
                .setText(R.id.tv_no_auth_count, getContext().getString(R.string.detonator_plan_manage_adapter_no_auth_count, plan.getUnAuthCount()))
                .setText(R.id.tv_max_delay, getContext().getString(R.string.detonator_plan_manage_adapter_max_delay, plan.getMaxDelay()))
                .setText(R.id.tv_min_delay, getContext().getString(R.string.detonator_plan_manage_adapter_min_delay, plan.getMinDelay()));
    }

    @Override
    public void setNewInstance(@Nullable List<PlanEntity> list) {
        super.setNewInstance(list);
        onSelectChange();
    }

    public List<PlanEntity> getSelectData() {
        List<PlanEntity> list = new ArrayList<>();
        for (PlanEntity entity : getData()) {
            if (entity.isSelect()) list.add(entity);
        }
        return list;
    }

    private void onSelectChange() {
        int detonatorCount = 0;
        int noAuthDetonatorCount = 0;
        int selectCount = 0;
        for (PlanEntity entity : getData()) {
            if (entity.isSelect()) {
                detonatorCount += entity.getCapCount();
                noAuthDetonatorCount += entity.getUnAuthCount();
                selectCount++;
            }
        }
        listener.onChange(selectCount, detonatorCount, noAuthDetonatorCount);
    }

    public void update(PlanEntity plan) {
        notifyItemChanged(getItemPosition(plan));
        onSelectChange();
    }

    public interface OnSelectChangeListener {
        void onChange(int planCount, int detonatorCount, int noAuthDetonatorCount);
    }

    public void selectAll() {
        for (PlanEntity entity : getData()) {
            entity.setSelect(true);
        }
        notifyDataSetChanged();
        onSelectChange();
    }

    public void unSelectAll() {
        for (PlanEntity entity : getData()) {
            entity.setSelect(false);
        }
        notifyDataSetChanged();
        onSelectChange();
    }
}
